package com.jwdf.uda.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

/**
 * Created by Administrator on 2016/7/1.
 */
public class BroadcastUtils {
    public static final String LOGIN_INVALID_ACTION = "com.jwdf.uda.login_invalid_action";
    public static final String MOBILE_ALREADY_REGISTER = "com.jwdf.uda.mobile_already_register";

    public static void registerLoginInvalidBroadcast(Context context,
                                                     BroadcastReceiver receiver) {
        registerReceiver(context, receiver, LOGIN_INVALID_ACTION);
    }

    public static void sendLoginInvalidBroadCast(Context context) {
        sendLocalBroadCast(context, LOGIN_INVALID_ACTION);
    }

    public static void registerMobileOccupyBroadcast(Context context,
                                                     BroadcastReceiver receiver) {
        registerReceiver(context, receiver, MOBILE_ALREADY_REGISTER);
    }

    public static void sendMobileOccupyBroadCast(Context context) {
        sendLocalBroadCast(context, MOBILE_ALREADY_REGISTER);
    }

    public static void sendLocalBroadCast(Context context, String action) {
        Intent intent = new Intent(action);
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
                .getInstance(context);
        localBroadcastManager.sendBroadcast(intent);
    }

    public static void registerReceiver(Context context,
                                        BroadcastReceiver receiver, String... actions) {
        IntentFilter intentFilter = new IntentFilter();
        for (String action : actions) {
            intentFilter.addAction(action);
        }
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
                .getInstance(context);
        localBroadcastManager.registerReceiver(receiver, intentFilter);
    }

    public static void unregisterReceiver(Context context,
                                          BroadcastReceiver receiver) {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
                .getInstance(context);
        localBroadcastManager.unregisterReceiver(receiver);
    }
}
